package game.classes.gameField;

import game.classes.interfaces.Block;
import game.classes.interfaces.Movable;
import game.classes.interfaces.Player;
import game.classes.interfaces.Usable;
import game.enums.Direction;
import game.enums.PlayerState;

public class gamePlayer implements Player{
	int x;
	int y;
	PlayerState state;
	Direction direction;
	gameArea list;
	int tickCount;
	
	public gamePlayer (int x,int y,Direction dir,gameArea list)
	{
		this.x=x;
		this.y=y;
		direction=dir;
		this.list=list;
		state = PlayerState.IDLE;
	}
	
	@Override
	public void moveUp() {
		// TODO Auto-generated method stub
		if(direction == Direction.UP)
		{
			if(tickCount==25)
			{
				if(this.y>0)
				{
					if(!getBlockAbove().isSolid())
					{
						if(((Movable)getBlockAbove()).isActive() && getBlockAbove() instanceof Movable)
						{	
							if(((Movable)getBlockAbove()).moveUp())
							{
								y--;
							}
						}
					}
				}
			
				tickCount=0;
			}
			if(tickCount<25)
			{
			tickCount++;
			}
		}
		else
		{
			setDirection(Direction.UP);
			tickCount=15;
		}
	}

	@Override
	public void moveLeft() {
		// TODO Auto-generated method stub
		if(direction == Direction.LEFT)
		{
			if(tickCount==25)
			{
				if(this.x>0)
				{
					if(!getBlockLeft().isSolid())
					{
						if(((Movable)getBlockLeft()).isActive() && getBlockLeft() instanceof Movable)
						{	
							if(((Movable)getBlockLeft()).moveLeft())
							{
								x--;
							}
						}
					}
				}
			
				tickCount=0;
			}
			if(tickCount<25)
			{
			tickCount++;
			}
		}
		else
		{
			setDirection(Direction.LEFT);
			tickCount=15;
		}
	}

	@Override
	public void moveRight() {
		// TODO Auto-generated method stub
		if(direction == Direction.RIGHT)
		{
			if(tickCount==25)
			{
				if(this.x<list.getMaxX()-1)
				{
					if(!getBlockRight().isSolid())
					{
						if(((Movable)getBlockRight()).isActive() && getBlockRight() instanceof Movable)
						{	
							if(((Movable)getBlockRight()).moveRight())
							{
								x++;
							}
						}
					}
				}
			
				tickCount=0;
			}
			if(tickCount<25)
			{
			tickCount++;
			}
		}
		else
		{
			setDirection(Direction.RIGHT);
			tickCount=15;
		}
	}

	@Override
	public void moveDown() {
		// TODO Auto-generated method stub
		if(direction == Direction.DOWN)
		{
			if(tickCount==25)
			{
				if(this.y<list.getMaxY()-1)
				{
					if(!getBlockBelow().isSolid())
					{
						if(((Movable)getBlockBelow()).isActive() && getBlockBelow() instanceof Movable)
						{	
							if(((Movable)getBlockBelow()).moveDown())
							{
								y--;
							}
						}
					}
				}
			
				tickCount=0;
			}
			if(tickCount<25)
			{
			tickCount++;
			}
		}
		else
		{
			setDirection(Direction.DOWN);
			tickCount=15;
		}
	}
		
	public Direction getDirection(){
		return direction;
	}
	
	public void setDirection(Direction dir){
		direction=dir;
	}
	
	public Block getBlockAbove() {
		// TODO Auto-generated method stub
		if(this.y==0)
		{
			return null;
		}
		return list.getBlockAtPos(x, y - 1);
	}

	@Override
	public Block getBlockRight() {
		// TODO Auto-generated method stub
		if(this.x+1<list.getMaxX())
		{
			return null;
		}
		return list.getBlockAtPos(x + 1, y);
	}

	@Override
	public Block getBlockLeft() {
		// TODO Auto-generated method stub
		if(this.x==0)
		{
			return null;
		}	
		return list.getBlockAtPos(x - 1, y);
	}

	@Override
	public Block getBlockBelow() {
		// TODO Auto-generated method stub
		if(this.y+1<list.getMaxY())
		{
			return null;
		}
		return list.getBlockAtPos(x, y + 1);
	}

	@Override
	public void activate() {
		if(direction == Direction.UP)
		{
			if(getBlockAbove() instanceof Usable)
			{
				((Usable) getBlockAbove()).activate();
			}
			else if(getBlockAbove() instanceof Movable)
			{
				((Movable) getBlockAbove()).activate();
			}
		}
		if(direction == Direction.RIGHT)
		{
			if(getBlockRight() instanceof Usable)
			{
				((Usable) getBlockRight()).activate();
			}
			else if(getBlockRight() instanceof Movable)
			{
				((Movable) getBlockRight()).activate();
			}
		}
		if(direction == Direction.DOWN)
		{
			if(getBlockBelow() instanceof Usable)
			{
				((Usable) getBlockBelow()).activate();
			}
			else if(getBlockBelow() instanceof Movable)
			{
				((Movable) getBlockBelow()).activate();
			}
		}
		if(direction == Direction.LEFT)
		{
			if(getBlockLeft() instanceof Usable)
			{
				((Usable) getBlockLeft()).activate();
			}
			else if(getBlockLeft() instanceof Movable)
			{
				((Movable) getBlockLeft()).activate();
			}
		}
	}

	@Override
	public int getX() {
		// TODO Auto-generated method stub
		return x;
	}

	@Override
	public int getY() {
		// TODO Auto-generated method stub
		return y;
	}

	@Override
	public void setX(int value) {
		// TODO Auto-generated method stub
		x=value;
	}

	@Override
	public void setY(int value) {
		// TODO Auto-generated method stub
		y=value;
	}

	@Override
	public PlayerState getState() {
		// TODO Auto-generated method stub
		return state;
	}

	@Override
	public void setState(PlayerState state) {
		// TODO Auto-generated method stub
		this.state = state;
	}
	
	public void tick()
	{
		if(tickCount==0)
		{
			
		}
		else if(tickCount < 25){
			tickCount++;
		}
	}


}
